package org.dromara.erp.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.domain.AuditDto;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.erp.domain.ErpWarehousing;
import org.dromara.erp.service.IErpWarehousingService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 采购入库
 *
 * @author Mr Lei
 * @date 2024-06-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/erp/warehousing")
public class ErpWarehousingController extends BaseController {

    private final IErpWarehousingService erpWarehousingService;

    /**
     * 查询采购入库列表
     */
    @SaCheckPermission("erp:warehousing:list")
    @GetMapping("/list")
    public TableDataInfo<ErpWarehousing> list(ErpWarehousing bo, PageQuery pageQuery) {
        return erpWarehousingService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出采购入库列表
     */
    @SaCheckPermission("erp:warehousing:export")
    @Log(title = "采购入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpWarehousing bo, HttpServletResponse response) {
        List<ErpWarehousing> list = erpWarehousingService.queryList(bo);
        ExcelUtil.exportExcel(list, "采购入库", ErpWarehousing.class, response);
    }

    /**
     * 获取采购入库详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("erp:warehousing:query")
    @GetMapping("/{id}")
    public R<ErpWarehousing> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(erpWarehousingService.queryById(id));
    }

    /**
     * 新增采购入库
     */
    @SaCheckPermission("erp:warehousing:add")
    @Log(title = "采购入库", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpWarehousing bo) {
        return toAjax(erpWarehousingService.insert(bo));
    }

    /**
     * 修改采购入库
     */
    @SaCheckPermission("erp:warehousing:edit")
    @Log(title = "采购入库", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpWarehousing bo) {
        return toAjax(erpWarehousingService.update(bo));
    }

    /**
     * 删除采购入库
     *
     * @param ids 主键串
     */
    @SaCheckPermission("erp:warehousing:remove")
    @Log(title = "采购入库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotNull(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(erpWarehousingService.deleteWithValidByIds(List.of(ids), true));
    }
    /*审核操作*/
    @SaCheckPermission("erp:warehousing:audit")
    @PutMapping("/audit")
    public R<Void> updateAudit(@RequestBody AuditDto dto){
        erpWarehousingService.updateAudit(dto);
        return R.ok();
    }
    /*获取默认标题*/
    @GetMapping("/title")
    public R<String> defaultTitle() {
        //根据统计当日列表数量，生成默认标题
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMdd");//日期格式化
        LocalDate today = LocalDate.now();//今天
        String formatDate = today.format(formatter);
        //生成标题
        String defTitle = erpWarehousingService.defaultTitle(formatDate);
        return R.ok("ok", "CG" + formatDate + defTitle);
    }
}
